﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Attendences.Framework.BusinessLogic;
using System.Data;
using System.Text.RegularExpressions;

public partial class Page_OtherUserList : System.Web.UI.Page
{
    public string SelID
    {
        get
        {
            if (Request.QueryString["SelID"] != null)
                return Request.QueryString["SelID"];
            return string.Empty;
        }
    }
    private string ReturnValueFormat
    {
        get
        {
            if (ViewState["ReturnValueFormat"] != null)
                return ViewState["ReturnValueFormat"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["ReturnValueFormat"] = value;
        }
    }
    private string NameTag
    {
        get
        {
            if (ViewState["NameTag"] != null)
                return ViewState["NameTag"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["NameTag"] = value;
        }
    }
    public string IDTag
    {
        get
        {
            if (ViewState["IDTag"] != null)
                return ViewState["IDTag"].ToString();
            return string.Empty;
        }
        set
        {
            ViewState["IDTag"] = value;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request["ReturnValueFormat"]))
            {
                ReturnValueFormat = Request["ReturnValueFormat"];
            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "wrong", "alert('传入参数错误');window.close();", true);
            }
            string[] Tags = ReturnValueFormat.Split(new char[] { '|' });
            if (Tags.Length >= 2)
            {
                foreach (string s in Tags)
                {
                    if (s.IndexOf(":DisplayName") > 0)
                    {
                        NameTag = s.Split(new char[] { ':' })[0];
                    }
                    if (s.IndexOf(":ID") > 0)
                    {
                        IDTag = s.Split(new char[] { ':' })[0];
                    }
                }

            }
            else
            {
                ClientScript.RegisterStartupScript(GetType(), "wrong", "alert('传入参数错误');window.close();", true);
            }
            DataTable dt = BLOtherUser.getAllUser();
            cblOtherUser.DataSource = dt;
            cblOtherUser.DataTextField = "DisplayName";
            cblOtherUser.DataValueField = "ID";
            cblOtherUser.DataBind();
            if (SelID != string.Empty)
            {
                string[] selectedIDs = SelID.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string str in selectedIDs)
                {
                    foreach (ListItem item in cblOtherUser.Items)
                    {
                        if (item.Value == str)
                            item.Selected = true;
                    }
                }
            }
        }
    }
    protected void lbtnOK_Click(object sender, EventArgs e)
    {
        string Names = "";
        string IDs = "";
        bool SelectNone = true;
        foreach (ListItem item in cblOtherUser.Items)
        {
            if (item.Selected)
            {
                SelectNone &= false;
                if (Names != "")
                    Names += ";";
                if (IDs != "")
                    IDs += ";";
                Names += item.Text;
                IDs += item.Value;
            }
        }
        string script = string.Format(@"window.dialogArguments.getElementById('{0}').value='{1}';
                                        window.dialogArguments.getElementById('{2}').value='{3}';
                                        window.close();",
                                        NameTag, Names, IDTag, IDs);
        ClientScript.RegisterStartupScript(GetType(), "OK", script, true);
    }
}